package cn.edu.demo.service;

import cn.edu.demo.mapper.CarMapper;
import cn.edu.demo.mapper.DriverMapper;
import cn.edu.demo.mapper.OrdersMapper;
import cn.edu.demo.pojo.*;

import cn.edu.demo.pojo.Passenger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    DriverMapper driverMapper;
    @Autowired
    CarMapper carMapper;

    /*
        获取最后的订单id
     */
    @Override
    //获取最后的订单值
    public String getLastOid() {
        OrdersExample example = new OrdersExample();
        example.setOrderByClause("order_ID DESC");
        return ordersMapper.selectByExample(example).get(0).getOrderId();
    }

    @Override
    //新创订单值
    public boolean newOrder(OrdersWithBLOBs bloBs) {
        return ordersMapper.insertSelective(bloBs) == 1 ? true : false;
    }

    @Override
    public boolean payOrder(String o_id, String money) {
        OrdersExample example = new OrdersExample();
        OrdersExample.Criteria criteria = example.or();
        criteria.andOrderIdEqualTo(o_id);
        Orders orders = ordersMapper.selectByExample(example).get(0);
        orders.setStatusPay("已支付");
        orders.setStatusOrder("已完成");
        return ordersMapper.updateByPrimaryKey(orders) != 0 ? true : false;
    }

    @Override
    public Orders getOrder(String o_id) {  //获取当前订单号
        OrdersExample example = new OrdersExample();
        return ordersMapper.selectByExample(example).get(0);
    }

    @Override
    public Orders sureOrders(String d_id, String d_point, String carnum, long phone, String d_name, String c_type) {
        OrdersExample example = new OrdersExample();
        OrdersExample.Criteria criteria = example.or();
        criteria.andOrderIdEqualTo(d_id);
        criteria.andOrderIdEqualTo(d_point);
        criteria.andOrderIdEqualTo(carnum);
        criteria.andDriverIdEqualTo(String.valueOf(phone));
        criteria.andDriverIdEqualTo(d_name);
        criteria.andCarIdEqualTo(c_type);
//        System.out.println(mapper.selectByExample(example).toString());
        return ordersMapper.selectByExample(example).size() != 0 ? ordersMapper.selectByExample(example).get(0) : null;
    }


    @Override
    public List<OrdersWithBLOBs> history(String uid) {
        OrdersExample example = new OrdersExample();
        OrdersExample.Criteria criteria = example.or();
        criteria.andPassengerIdEqualTo(uid);
        return ordersMapper.selectByExampleWithBLOBs(example).size() != 0 ? ordersMapper.selectByExampleWithBLOBs(example) : null;
    }

    @Override
    public Orders cancel(String o_id, String time) {
        return null;
    }

//    @Override
//    public List<OrdersWithBLOBs> sure(String o_id) {
//        OrdersExample example = new OrdersExample();
//        OrdersExample.Criteria criteria = example.or();
//        criteria.andOrderIdEqualTo(o_id);
//        return ordersMapper.selectByExampleWithBLOBs(example).size() != 0 ? ordersMapper.selectByExampleWithBLOBs(example) : null;
//    }

    @Override
    public Passenger com(String o_id, String comment) {
        return null;
    }

//    @Override
//    public Driver sure(String d_name, long phone) {
//        return null;
//    }
//
    @Override
    public List<OrdersWithBLOBs> sure(String o_id) {
        OrdersExample example = new OrdersExample();
        OrdersExample.Criteria criteria = example.or();
        criteria.andPassengerIdEqualTo(o_id);
        return ordersMapper.selectByExampleWithBLOBs(example).size() != 0 ? ordersMapper.selectByExampleWithBLOBs(example) : null;

    }
//
//    @Override
//    public Driver sure(String d_name, long phone) {
//        return null;
//    }
}
